package com.djb.controller;

import com.djb.entity.Goods;
import com.djb.entity.User;
import com.djb.entity.Yxj;
import com.djb.entity.Zt;
import com.djb.service.AllService;
import com.djb.vo.Vo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.naming.Name;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class AllController {

    @Autowired
    AllService allService;

    @RequestMapping("tologin")
    public String tologin(User user,Model model){
        model.addAttribute("user",user);
        return "login";
    }

    @RequestMapping("login")
    public String login(User user,HttpSession session){
        User login = allService.login(user);
        if(login!=null){
            session.setAttribute("user",login);
            return "redirect:list";
        }
        return "";
    }

    @RequestMapping("list")
    public String list(Vo vo, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2")  Integer pageSize, HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        Integer role = user.getRole();
        vo.setId(id);
        vo.setRole(role);
        PageInfo<Goods> info = allService.list(vo, pageNum, pageSize);
        model.addAttribute("info",info);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("vo",vo);
        return "list";
    }

    @ResponseBody
    @RequestMapping("upFile")
    public String upfile(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            String path="d:/love/";
            String oname = file.getOriginalFilename();
            String nname = UUID.randomUUID() + oname.substring(oname.lastIndexOf("."));
            File file1 = new File(path,nname);
            file.transferTo(file1);
            return "/love/"+nname;
        }
        return null;
    }

    @RequestMapping("toadd")
    public String toadd(Goods goods,Model model){
        model.addAttribute("goods",goods);
        return "add";
    }
    @RequestMapping("add")
    public String add(Goods goods, BindingResult result){
        allService.addGoods(goods);
        return"redirect:list";

    }

    @ResponseBody
    @RequestMapping("listu")
    public List<User> listu(){
        return allService.listu();
    }

    @ResponseBody
    @RequestMapping("listz")
    public List<Zt> listz(){
        return allService.listz();
    }

    @ResponseBody

    @RequestMapping("listy")
    public List<Yxj> listy(){
        return allService.listy();
    }

    @ResponseBody
    @RequestMapping("upgoods")
    public int upgoods(Integer id){
        Goods goods = allService.getByid(id);
        return allService.upgoods(id);

    }

}
